//
//  AboutView.m
//  MobileZIPCodes
//
//  Created by Andreas Schwelling on 03.12.07.
//	http://www.andi.de/zipcodes/
// 
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.
//

#import "AboutView.h"


@implementation AboutView


- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	_rect = rect;
	_navBar  = [[UINavigationBar alloc] init];
	[_navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 48.0f)];
	[_navBar setDelegate:self];
	[_navBar enableAnimation];
	
	[_navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: @"About"]];	
	
	[self addSubview:_navBar];
	
	_table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y+48.0f, rect.size.width, rect.size.height-49.0f-48.0f)];
	[_table setDataSource:self];
	[_table setDelegate:self];
	
	
	
	[_table reloadData];
	
	[self addSubview:_table];
	
	
	
	
	return self;
	
}

-(void)dealloc {
	[_navBar release];
	
	[super dealloc];
	
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

#pragma mark - delegated methods -


- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 6;
}


- (float)preferencesTable:(UIPreferencesTable *)table 
			 heightForRow:(int)row 
				  inGroup:(int)group 
	   withProposedHeight:(float)proposed 
{
	
	if (group == 0 && row == -1) return 10;
	if (group == 0 && row == 0) return 65;
	if (group == 1 && row == -1) return 5;
	if (group == 1 && row == 0) return 22;
	if (group == 1 && row == 1) return 22;
	if (group == 1 && row == 2) return 25;
	
	if (group == 2 && row == -1) return 10;
	if (group == 2 && row == 0) return 60;
	
	
	if (group == 4 && row == -1) return 20;
	if (group == 4 && row == 0) return 25;
	
	if (group == 5 && row == -1) return 2;
	if (group == 5 && row == 0) return 255;
	
	/*
	 
	 
	 if (group == 2 && row == -1) return 10;
	 if (group == 2 && row == 0) return 25;
	 if (group == 3 && row == -1) return 10;
	 
	 if (group == 4 && row == -1) return 10;
	 if (group == 4 && row == 0) return 25;
	 if (group == 5 && row == -1) return 10;
	 
	 
	 if (group == 0 || group == 2) {
	 return 20;
	 }
	 */
	
	
    return proposed;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	switch (group)
  {
	  case 0: return 1;
	  case 1: return 3;
	  case 2:	return 1;
	  case 3: return 3;
	  case 4: return 1;
	  case 5: return 1;
		  
  }
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	
	if (group == 0)
	  {
		  
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@""];
		  [ cell setDrawsBackground: NO ];
		  [cell _setDrawAsGroupTitle:NO];
		  
		  UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 80.0f, 80.0f)];
		  [img setImage:[UIImage applicationImageNamed:@"icon.png"]];
		  [cell addSubview:img];
		  [img release];
		  
		  [cell setAlignment:2];
		  return [cell autorelease];
	  }				
	else if (group == 1)
	  {
		  
		  switch (row) {
			  case 0: {
				  
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"ZIPcodes"];
				  //[cell setAlignment:2];
				  [ cell setDrawsBackground: NO ];
				  [cell _setDrawAsGroupTitle:YES];
				  [cell setAlignment:2];
				  return [cell autorelease];
			  }
			  case 1: {
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"ver 0.1"];
				  //[cell setAlignment:2];
				  [ cell setDrawsBackground: NO ];
				  [cell _setDrawAsGroupTitle:YES];
				  [cell setAlignment:2];
				  return [cell autorelease];
			  }
			  case 2: {
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"developed by sAkYa"];
				  //[cell setAlignment:2];
				  [ cell setDrawsBackground: NO ];
				  [cell _setDrawAsGroupTitle:YES];
				  [cell setAlignment:2];
				  return [cell autorelease];
			  }
		  }
		  
		  
		  
		  
		  
	  } 
	else if (group == 2)
	  {
		  
		  
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@"A free Zip code lookup tool\nwith offline data - browse\nZip Codes without necessity to be online"];
		  [ cell setDrawsBackground: NO ];
		  [cell _setDrawAsGroupTitle:YES];
		  return [cell autorelease];
		  
		  
		  
	  } 
	else if (group == 3) 
	  {
		  switch (row) {
			  case 0: {
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"contact by mail"];
				  [cell setAlignment: 2];
				  [cell setShowDisclosure:YES];
				  [cell setDisclosureStyle:2];
				  return [cell autorelease];
			  }
			  case 1: {
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"visit homepage"];
				  [cell setAlignment: 2];
				  [cell setShowDisclosure:YES];
				  [cell setDisclosureStyle:2];
				  return [cell autorelease];
				  
			  }
			  case 2: {
				  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				  [cell setTitle:@"donate"];
				  [cell setAlignment: 2];
				  [cell setShowDisclosure:YES];
				  [cell setDisclosureStyle:2];
				  return [cell autorelease];
				  
			  }
		  }
		  
	  }
	
	else if (group == 4) 
	  {
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@"Annotation about data pool"];
		  [ cell setDrawsBackground: NO ];
		  [cell _setDrawAsGroupTitle:YES];
		  [cell setAlignment:2];
		  return [cell autorelease];
		  
	  }	
	else if (group == 5) 
	  {
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@"The Zip codes and city data found in this application come from open and freely accessible sources.\n\nI've neither created the contents nor checked every single item for correctitude - so please don't blame me if you consider something to be faulty;)!\n\nIf you want to see the Zip code data for your country included in ZIPcodes.app, please contact me providing information about possible data sources."];
		  
		  [ cell setDrawsBackground: NO ];
		  [cell _setDrawAsGroupTitle:YES];
		  
		  return [cell autorelease];
	  }
	
}

- (BOOL)preferencesTable:(UIPreferencesTable *)aTable isLabelGroup:(int)group {
	if (group == 2 || group == 5) return YES;
	return NO;
}


- (void)tableRowSelected:(NSNotification *)notification
{
	//NSLog(@"row: %d",[_table selectedRow]);
	
	if ([_table selectedRow] == 9) {
		
		MobileZIPCodesApp *mzca = [MobileZIPCodesApp sharedInstance];
		
		[mzca openURL:[NSURL URLWithString:@"mailto:andi@andi.de?Subject=Regarding%20ZIPcodes.app"]];
		
		
	}
	
	if ([_table selectedRow] == 10) {
		
		MobileZIPCodesApp *mzca = [MobileZIPCodesApp sharedInstance];
		
		[mzca openURL:[NSURL URLWithString:@"http://www.andi.de/zipcodes/"]];
		
		
	}
	
	if ([_table selectedRow] == 11) {
		
		MobileZIPCodesApp *mzca = [MobileZIPCodesApp sharedInstance];
		
		[mzca openURL:[NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=andreas%40schwelling%2ede&item_name=ZIPcodes.app%20donation&no_shipping=0&no_note=1&tax=0&currency_code=EUR&bn=PP%2dDonationsBF&charset=UTF%2d8"]];
		
		
	}
	
	
}



@end
